from PyQt5 import uic
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import pyqtgraph as pg
from ga_algorithm import *
import xlrd
from threading import Thread, RLock
from multiprocessing import Process
from queue import Queue

class Ui_NetInfo(QWidget):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.location_demandTable_Ui = uic.loadUi("location_demandTable.ui")
        self.initNetInfo()

    def initNetInfo(self):
        data = xlrd.open_workbook('Location.xlsx')
        dataDict = self.readDataFromXls(data)
        row_number = len(dataDict['NodeCoor'])
        self.location_demandTable_Ui.resize(600, 700)
        self.location_demandTable_Ui.ok.clicked.connect(self.location_demandTable_Ui.close)
        self.location_demandTable_Ui.cancel.clicked.connect(self.location_demandTable_Ui.close)
        self.location_demandTable_Ui.location_demandData.setRowCount(row_number)
        self.location_demandTable_Ui.location_demandData.setEditTriggers(QAbstractItemView.NoEditTriggers)
        header_labels = []
        for i in range(0, row_number):
            header_labels.append('Node' + str(i))
        self.location_demandTable_Ui.location_demandData.setVerticalHeaderLabels(header_labels)
        self.setViewTable(dataDict)

    def readDataFromXls(self, data):
        location = data.sheets()[0]
        # 用字典存储所有参数 -- 配送中心坐标、顾客坐标、顾客需求、到达时间窗口、服务时间、车型载重量
        dataDict = {}
        # 节点坐标，节点0是配送中心的坐标
        dataDict['NodeCoor'] = []
        for value in range(0, location.nrows):
            Loc = (location.row_values(value)[0], location.row_values(value)[1])
            dataDict['NodeCoor'].append(Loc)
        # 需求表，将配送中心的需求设置为0
        dataDict['Demand'] = []
        for value in range(0, location.nrows):
            demand = location.row_values(value)[2]
            dataDict['Demand'].append(demand)
        return dataDict

    def setViewTable(self, dataDict):
        for row in range(0, len(dataDict['NodeCoor'])):
            xItem = QTableWidgetItem(str(dataDict['NodeCoor'][row][0]))
            self.location_demandTable_Ui.location_demandData.setItem(row, 0, xItem)
            yItem = QTableWidgetItem(str(dataDict['NodeCoor'][row][1]))
            self.location_demandTable_Ui.location_demandData.setItem(row, 1, yItem)
            dItem = QTableWidgetItem(str(dataDict['Demand'][row]))
            self.location_demandTable_Ui.location_demandData.setItem(row, 2, dItem)


class Ui_CarInfo(QWidget):
    def __init__(self, MainParam, *args, **kwargs):
        super().__init__(*args, **kwargs)

        """初始化本部参数"""
        self.CarInfoParams = {}
        self.CarInfoParams['CarIsChangeAble'] = MainParam['CarIsChangeAble']
        self.CarInfoParams['CarIsNotChangeAble'] = MainParam['CarIsNotChangeAble']
        self.CarInfoParams['CarNumSet'] = MainParam['CarNumSet']
        self.CarInfoParams['OneTypeCar'] = MainParam['OneTypeCar']
        self.CarInfoParams['TwoTypeCar'] = MainParam['TwoTypeCar']
        self.CarInfoParams['Load_1'] = MainParam['Load_1']
        self.CarInfoParams['Load_2'] = MainParam['Load_2']
        self.CarInfoParams['CarSpeed'] = MainParam['CarSpeed']
        self.CarInfo_Ui = uic.loadUi("CarInfo.ui")
        self.initCarInfo()

    def initCarInfo(self):

        """设置点击相应"""
        self.CarInfo_Ui.ChangeAble.clicked.connect(lambda: self.setIsChangeableVisible())
        self.CarInfo_Ui.UnChangeAble.clicked.connect(lambda: self.setNotChangeableVisible())
        self.CarInfo_Ui.OneType.clicked.connect(lambda: self.setOneTypeVisible())
        self.CarInfo_Ui.TwoType.clicked.connect(lambda: self.setTwoTypeVisible())

        """设置必然事件"""
        if self.CarInfo_Ui.ChangeAble.isChecked and self.CarInfo_Ui.UnChangeAble.isChecked == False:
            self.setIsChangeableVisible()
        elif self.CarInfo_Ui.UnChangeAble.isChecked and self.CarInfo_Ui.ChangeAble.isChecked == False:
            self.setNotChangeableVisible()
        else:
            pass

        if self.CarInfo_Ui.OneType.isChecked:
            self.setOneTypeVisible()
        elif self.CarInfo_Ui.TwoType.isChecked:
            self.setTwoTypeVisible()
        else:
            pass

        """关闭窗口"""
        self.CarInfo_Ui.Ok.clicked.connect(lambda: self.CloseAndSave())
        self.CarInfo_Ui.Cancel.clicked.connect(self.CarInfo_Ui.close)

    def CloseAndSave(self):
        self.deliverPara()
        self.CarInfo_Ui.close()

    def setIsChangeableVisible(self):
        self.CarInfo_Ui.CarNum.setEnabled(False)
        self.CarInfo_Ui.TwoType.setEnabled(True)
        self.CarInfo_Ui.Load2.setEnabled(True)

    def setNotChangeableVisible(self):
        self.CarInfo_Ui.UnChangeAble.setCheckable(True)
        self.CarInfo_Ui.CarNum.setEnabled(True)
        self.CarInfo_Ui.OneType.setCheckable(True)
        self.CarInfo_Ui.TwoType.setEnabled(False)
        self.CarInfo_Ui.Load2.setEnabled(False)

    def setOneTypeVisible(self):
        self.CarInfo_Ui.Load2.setEnabled(False)

    def setTwoTypeVisible(self):
        self.CarInfo_Ui.Load2.setEnabled(True)

    def deliverPara(self):
        self.CarInfoParams['CarIsChangeAble'] = self.CarInfo_Ui.ChangeAble.isChecked()
        self.CarInfoParams['CarIsNotChangeAble'] = self.CarInfo_Ui.UnChangeAble.isChecked()
        self.CarInfoParams['CarNumSet'] = self.CarInfo_Ui.CarNum.value()
        self.CarInfoParams['OneTypeCar'] = self.CarInfo_Ui.OneType.isChecked()
        self.CarInfoParams['TwoTypeCar'] = self.CarInfo_Ui.TwoType.isChecked()
        self.CarInfoParams['Load_1'] = self.CarInfo_Ui.Load1.text()
        self.CarInfoParams['Load_2'] = self.CarInfo_Ui.Load2.text()
        self.CarInfoParams['CarSpeed'] = self.CarInfo_Ui.Speed.value()


class Ui_UnloadTime(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.UnloadTimePara = 0
        self.UnloadTime_Ui = uic.loadUi("UnloadTime.ui")
        self.initUnloadTime()

    def initUnloadTime(self):
        self.UnloadTime_Ui.Ok.clicked.connect(lambda: self.CloseAndSave())
        self.UnloadTime_Ui.Cancel.clicked.connect(self.UnloadTime_Ui.close)

    def CloseAndSave(self):
        self.UnloadTimePara = self.UnloadTime_Ui.UnloadTime.value()
        self.UnloadTime_Ui.close()


class Ui_TimeLimit(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.TimeLimitPara = 0
        self.TimeLimit_Ui = uic.loadUi("TimeLimit.ui")
        self.initTimeLimit()

    def initTimeLimit(self):
        self.TimeLimit_Ui.Ok.clicked.connect(lambda: self.CloseAndSave())
        self.TimeLimit_Ui.Cancel.clicked.connect(self.TimeLimit_Ui.close)

    def CloseAndSave(self):
        self.TimeLimitPara = self.TimeLimit_Ui.TimeLimit.value()
        self.TimeLimit_Ui.close()


class Ui_LengthLimit(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.LengthLimitPara = 0
        self.LengthLimit_Ui = uic.loadUi("LengthLimit.ui")
        self.initLengthLimit()

    def initLengthLimit(self):
        self.LengthLimit_Ui.Ok.clicked.connect(lambda: self.CloseAndSave())
        self.LengthLimit_Ui.Cancel.clicked.connect(self.LengthLimit_Ui.close)

    def CloseAndSave(self):
        self.LengthLimitPara = self.LengthLimit_Ui.LengthLimit.value()
        self.LengthLimit_Ui.close()
